import { api_getDictTreeData } from '@/api/app';

const useAppStore = defineStore('app', {
    state: () => ({
        token: '', // 通过url接受token，并存储

        
        // 字典数据
        isDictDataLoaded: false,
        dictData: {},

        regionData: {},

        urlData: {}, // 参数
    }),

    actions: {
        // 存储token
        setToken(data) {
            this.token = data;
        },

        // 获取字典数据
        getDictData() {
            return new Promise(resolve => {
                api_getDictTreeData().then(res => {
                    const resData = res.data;
                    resData.forEach(item => {
                        const py = pinyin(item.dictName, { 
                            toneType: 'none',
                            pattern: 'initial',
                            type: 'array'
                        });
                        const key = py.join('');

                        this.dictData[key] = {
                            id: item.id,
                            label: item.dictName,
                            list: (item.children && item.children.length) ? item.children.map(child => {
                                return {
                                    id: child.id,
                                    label: child.columnName,
                                    value: child.columnCode
                                }
                            }) : []
                        }
                    });

                    this.isDictDataLoaded = true;

                    resolve(res);
                });
            })
        },

        setReginData(data = {}) {
            this.regionData = data;
        },

        // 设置网页标题
        setTitle(title) {            
            if (title) {
                document.title = title;
            } else {
                document.title = 'APP地图';
            }
        },

        setUrlData(data) {
            this.urlData = data;
        }
    }
});

export default useAppStore;
